package com.stepforward.me;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class GoFavRoute extends Activity{
	static final int DATE_DIALOG_ID = 0;
	static final int TIME_DIALOG_ID = 1;
	private Button mPickDate;
	private Button mPickTime;
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;
	private EditText from;
	private EditText to;
	private Button edit;
	private String name;
	MyFavoriteHelper dbHelper;
	Boolean flag;
		public int getmYear() {
			return mYear;
		}

		public void setmYear(int mYear) {
			this.mYear = mYear;
		}

		public int getmMonth() {
			return mMonth;
		}

		public void setmMonth(int mMonth) {
			this.mMonth = mMonth;
		}

		public int getmDay() {
			return mDay;
		}

		public void setmDay(int mDay) {
			this.mDay = mDay;
		}

		public int getmHour() {
			return mHour;
		}

		public void setmHour(int mHour) {
			this.mHour = mHour;
		}

		public int getmMinute() {
			return mMinute;
		}

		public void setmMinute(int mMinute) {
			this.mMinute = mMinute;
		}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbHelper=new MyFavoriteHelper(this);
		flag=false;
		setContentView(R.layout.go);
		from = (EditText) findViewById(R.id.from);
		to = (EditText) findViewById(R.id.to);
		edit=(Button)findViewById(R.id.favedit);
		to.setEnabled(false);
		from.setEnabled(false);
		// add a click listener to the button    
		mPickDate = (Button) findViewById(R.id.favDate);
		mPickDate.setOnClickListener(new View.OnClickListener() {       
			public void onClick(View v) {             
				showDialog(DATE_DIALOG_ID);            }        });
		// get the current date      
		final Calendar c = Calendar.getInstance();    
		mYear = c.get(Calendar.YEAR);    
		mMonth = c.get(Calendar.MONTH);     
		mDay = c.get(Calendar.DAY_OF_MONTH);  
		// display the current date (this method is below)
		mPickTime = (Button) findViewById(R.id.favTime);
		mPickTime.setOnClickListener(new View.OnClickListener() {        
			 public void onClick(View v) {        
				 showDialog(TIME_DIALOG_ID);   
				 }        });

		// get the current time
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		updateDisplay(); 
		//Load in selected Fav
		Bundle extras = getIntent().getExtras();
		String departure = extras.getString("departure");
		String arrival = extras.getString("arrival");
		name=extras.getString("name");
		from.setText(departure);
		to.setText(arrival);
	

	}
	 // the callback received when the user "sets" the date in the dialog  
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {        
		@Override
		public void onDateSet(android.widget.DatePicker view, int year,
				int monthOfYear, int dayOfMonth) {
			// TODO Auto-generated method stub
			mYear = year;               
			mMonth = monthOfYear;         
			mDay = dayOfMonth;          
			updateDisplay();    
			
		}            };
		private TimePickerDialog.OnTimeSetListener mTimeSetListener =    new TimePickerDialog.OnTimeSetListener() {    
			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				mHour = hourOfDay;          
				mMinute = minute;         
				updateDisplay();  
			}    };
			@Override
			protected Dialog onCreateDialog(int id) {  
				switch (id) {    case DATE_DIALOG_ID:       
					return new DatePickerDialog(this,         
							mDateSetListener,                
							mYear, mMonth, mDay);   
				case TIME_DIALOG_ID:        return new TimePickerDialog(this,             
						mTimeSetListener, mHour, mMinute, false);	
				} 
				return null;}

	 // updates the date in the TextView   
	private void updateDisplay() {       
	

		 TextView dataText = (TextView) findViewById(R.id.favDateView);
		 dataText.setText(new StringBuilder()              
        .append(mMonth + 1).append("-")
        .append(mDay).append("-")
        .append(mYear).append(" "));
		 
		 TextView timeText = (TextView) findViewById(R.id.favTimeView);
		 timeText.setText(new StringBuilder()           
			.append(pad(mHour)).append(":")      
			.append(pad(mMinute)));
		 }
	private static String pad(int c) {    
		if (c >= 10)       
			return String.valueOf(c);  
		else       
			return "0" + String.valueOf(c);}
	public void editButtonClicked(View view)

	{
		if(flag==false)
		{

			edit.setText("Save");
			from.setEnabled(true);
			to.setEnabled(true);
			flag=true;

		}
		else
		{
			myFavorite myfav=new myFavorite(from.getText().toString(), to.getText().toString(), name);
			dbHelper.UpdateFav(myfav);
			
			edit.setText("Edit");
			flag=false;
			
			from.setText(dbHelper.getDepByName(name));
			to.setText(dbHelper.getArrByName(name));
			from.setEnabled(false);
			to.setEnabled(false);
		}
		
		
		
	}
	public void goButtonClicked(View view)
	{
		 Intent intent = new Intent();
		 intent.setAction("TripDetail");
		 intent.putExtra("departure",from.getText().toString() );
		 intent.putExtra("arrival",to.getText().toString());

		
			// intent.putExtra("hour", mHour+"");
			 if (mHour>=12){
				 mHour = mHour -12;
				 intent.putExtra("hour", mHour+"");
				 intent.putExtra("ampm","pm" );
			 }
			 if(mHour == 12){
				 intent.putExtra("hour", "12");
				 intent.putExtra("ampm","pm" );
			 }
				if(mHour <12){
					 intent.putExtra("hour", mHour+"");
					 intent.putExtra("ampm","am" );
				 }
							 
		 intent.putExtra("minute",mMinute+"" );
		
		 startActivity(intent);
		 }        
 
	

}



